package com.eastfair.projectcore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import com.eastfair.projectcore.dao.ConfigAttributeMapper;
import com.eastfair.projectcore.dto.ConfigAttributeDTO;
import com.eastfair.projectcore.entity.ConfigAttribute;
import com.eastfair.projectcore.service.ConfigAttributeService;
import com.eastfair.projectcore.vo.ConfigAttributeVO;
import com.eastfair.boot.service.SuperCacheServiceImpl;
import com.eastfair.cache.configattribute.ConfigAttributeCacheKeyBuilder;
import com.eastfair.core.base.R;
import com.eastfair.core.model.CacheKeyBuilder;
import com.eastfair.uid.model.SelfUidDto;
import com.eastfair.uid.service.SelfUidService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 配置模块属性表
 * </p>
 *
 * @author zjn
 * @date 2021-08-05
 */
@Slf4j
@Service
public class ConfigAttributeServiceImpl extends SuperCacheServiceImpl<ConfigAttributeMapper, ConfigAttribute> implements ConfigAttributeService {

//    @Autowired
//    SelfUidService selfUidService;

    @Override
    protected CacheKeyBuilder cacheKeyBuilder() {
        return new ConfigAttributeCacheKeyBuilder();
    }

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(ConfigAttribute model) {
        //雪花ID,不需要分库分表
//        SelfUidDto selfUidDto = new SelfUidDto();
//        model.setId(selfUidService.getUid(selfUidDto));
        model.setId(DefaultUidGenerator.getUid(0L));
        return R.successDef();
    }

    @Override
    public List<ConfigAttributeVO> listByEntityClass(ConfigAttributeDTO configAttributeDTO) {
        QueryWrapper<ConfigAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ConfigAttribute::getEntityClass, configAttributeDTO.getEntityClass())
                .orderByAsc(ConfigAttribute::getSequence);
        List<ConfigAttribute> list = list(queryWrapper);
        return doToVos(list);
    }

    public ConfigAttributeVO doToVo(ConfigAttribute configAttribute) {
        ConfigAttributeVO configAttributeVo = new ConfigAttributeVO();
        BeanUtils.copyProperties(configAttribute, configAttributeVo);
        configAttributeVo.setId(configAttribute.getId());
        return configAttributeVo;
    }

    public List<ConfigAttributeVO> doToVos(List<ConfigAttribute> configAttributeList) {
        if (configAttributeList == null || configAttributeList.isEmpty()) {
            return null;
        }
        List<ConfigAttributeVO> result = new ArrayList<>();
        for (ConfigAttribute configAttribute : configAttributeList) {
            result.add(doToVo(configAttribute));
        }
        return result;
    }
}
